package com.project_test.kun.travelling_1st.HomePage.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.project_test.kun.travelling_1st.HomePage.Util.URL;
import com.project_test.kun.travelling_1st.HomePage.category.HomePHouseListItem;
import com.project_test.kun.travelling_1st.R;
import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * Created by Administrator on 16-10-18.
 */
public class HomePHouseListAdapter extends BaseAdapter {
    private Context mContext;
    private List<HomePHouseListItem> data;
    private Bitmap radStar;
    private Bitmap grayStar;
    public HomePHouseListAdapter(Context context, List<HomePHouseListItem> data) {
        mContext = context;
        this.data = data;
        radStar= URL.twoCollect(mContext, R.drawable.icon_star_selected,10,10);
        grayStar=URL.twoCollect(mContext,R.drawable.icon_star_unselect,10,10);
    }

    @Override
    public int getCount() {
        return data!=null?data.size():0;
    }

    @Override
    public HomePHouseListItem getItem(int i) {
        return data.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int postion, View view, ViewGroup viewGroup) {
        ViewHolder holder=null;
        if (view==null){
            view= LayoutInflater.from(mContext).inflate(R.layout.item_activity_homep_house,viewGroup,false);
            holder=new ViewHolder();
            holder.picture= (ImageView) view.findViewById(R.id.item_picture_homep_house);
            holder.address= (TextView) view.findViewById(R.id.item_address_homep_house);
            holder.max_person= (TextView) view.findViewById(R.id.item_max_person_homep_house);
            holder.name= (TextView) view.findViewById(R.id.item_name_homep_house);
            holder.price= (TextView) view.findViewById(R.id.item_price_homep_house);
            holder.scores= (LinearLayout) view.findViewById(R.id.item_scores_homep_house);
            holder.item_listener= (LinearLayout) view.findViewById(R.id.item_linsener);
            view.setTag(holder);
        }else{
            holder= (ViewHolder) view.getTag();
        }
        //赋值

        HomePHouseListItem houseListItem = data.get(postion);
        Picasso.with(mContext).load(houseListItem.getPicture()).into(holder.picture);
        holder.address.setText(houseListItem.getAddress());
        holder.max_person.setText("可住"+houseListItem.getMax_person()+"人");
        holder.name.setText(houseListItem.getName());
        holder.price.setText(houseListItem.getPrice()+"");
        for (int i = 0; i < houseListItem.getScores(); i++) {
           ImageView child= (ImageView) holder.scores.getChildAt(i);
            child.setImageBitmap(radStar);
        }
        for (int i=houseListItem.getScores();i<5 ; i++){
            ImageView child= (ImageView) holder.scores.getChildAt(i);
            child.setImageBitmap(grayStar);
        }
        final int hid=houseListItem.getHid();
        LinearLayout item_linsener = holder.item_listener;
        item_linsener.setTag(hid);
        item_linsener.setOnClickListener((View.OnClickListener) mContext);
        return view;
    }
    static  class ViewHolder{
//        "name": "乐聚派别墅轰趴馆，承接公司聚会，家庭聚会，同学聚会。",
//                "hid": "21401",
//                "price": "1980.00",
//                "buy_type": "58",
//                "max_person": "18",
//                "address": "北京市房山区",
//                "scores": "4.00",
//                "lng": "116.158233",
//                "lat": "39.730491",
//                "picture": "http://img1.handybest.com/Uploads/Hotel/920/16/08/579ef9aaf24f9.jpg!phone"
                private   ImageView picture;
                private TextView name;
                private TextView  price;
                  private TextView max_person;
                  private TextView address;
                         private LinearLayout scores;
            private LinearLayout item_listener;

    }
}
